//
//  CarLocationViewController.m
//  CarCare
//
//  Created by ileo on 14/12/27.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "CarLocateViewController.h"
#import "MapCarSearch.h"
#import "RefreshMgr.h"
#import "BaseWebViewController.h"

@interface CarLocateViewController ()

@property (nonatomic, strong) MapCarSearch *mapLocate;
@property (nonatomic, strong) RefreshMgr *refreshMgr;
//获取寻车路线类型
@property (nonatomic , assign) AMapRoutePlanningType searchLineType;
@property (nonatomic , strong) UIView * searchWayView;
@property (nonatomic , strong) NSMutableArray * searchLineTitleArr;

@property (nonatomic , strong) UIView  * navBackView;

@end

@implementation CarLocateViewController

-(void)dealloc{
    [self.mapLocate.mapView removeFromSuperview];
    self.mapLocate.mapView = nil;
    self.mapLocate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CommentHelper useFunction:Use_CarLoc];
    
    self.title = @"车辆位置";
    
    [self createNavView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mapLocate = [[MapCarSearch alloc] init];
    self.mapLocate.mapView.frame = self.view.bounds;
    [self.view addSubview:self.mapLocate.mapView];
    
//    self.searchLineType = AMapRoutePlanningTypeDrive;
    //    [self addSearLineTypeWayView];
    
    __weak __typeof(self) wself = self;
    
    [Factory buttonWithCenter:CGPointMake(40, __MAIN_HEIGHT_64 - 50) withImage:ImageNamed(@"search_car") click:^{
        [wself.mapLocate showCarLoc];
    } onView:self.view];
    [Factory buttonWithCenter:CGPointMake(40, __MAIN_HEIGHT_64 - 100) withImage:ImageNamed(@"search_user") click:^{
        [wself.mapLocate showUserLoc];
    } onView:self.view];
    
    self.refreshMgr = [RefreshMgr sharedInstance];
    [self refreshCarLocation];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCarLocation) name:REFRESH_CARLOCATION object:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1 block:^{
        [wself.mapLocate wayToLoc:self.searchLineType];
    } repeats:NO];
    
    //爱代驾
    
    //    [Factory buttonWithCenter:CGPointMake(__MAIN_WIDTH  - 40, __MAIN_HEIGHT_64 - 50) withImage:ImageNamed(@"car_drivingService") click:^{
    //
    //        BaseWebViewController *drivingServiceVC = [[BaseWebViewController alloc] initWithURL:@"http://h.aidaijia.com/redirect?s=f04982461482f92381ded238fb4126af"];
    //
    //        [drivingServiceVC setupNavBack];
    //        [wself.navigationController pushViewController:drivingServiceVC animated:YES];
    //        drivingServiceVC.title = @"代驾服务";
    //        [UmengMgr event:ICZ_CarPosition_DrivingService];
    //
    //    } onView:self.view];
    
    // Do any additional setup after loading the view.
}
- (void)createNavView
{
    self.navBackView = [[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH / 2, 30)] resetBackgroundColor:[UIColor clearColor]];
    UIImage * runImage = ImageNamed(@"driverImage");
    [Factory imageViewWithCenter:CGPointMake(runImage.size.width , self.navBackView.height / 2) image:runImage onView:self.navBackView];
    [Factory imageViewWithCenter:CGPointMake(self.navBackView.width / 2, self.navBackView.height / 2) image:ImageNamed(@"busImage") onView:self.navBackView];
    [Factory imageViewWithCenter:CGPointMake(self.navBackView.width - runImage.size.width , self.navBackView.height / 2) image:ImageNamed(@"runImage") onView:self.navBackView];
    for (int index = 0; index < 3; index ++) {
        //        CGFloat lineH = 38;
        //        CGFloat lineY = (searchWayViewH - lineH) / 2;
        //        CGFloat lineW = 1;
        //        CGFloat lineX = __MAIN_WIDTH / 3 * (index + 1);
        //        if (index < self.searchLineTitleArr.count - 1) [Factory viewWithFrame:CGRectMake(lineX, lineY, lineW, lineH) bgColor:COLOR_HEXSTRING(@"#E8E8E8") onView:self.searchWayView];
        //
        //        CGFloat smallSearchViewW = __MAIN_WIDTH / 3;
        //        CGFloat smallSearchViewX = smallSearchViewW * index;
        //        UIView * smallSearchView = [self searchThreeView:CGRectMake(smallSearchViewX, 0, smallSearchViewW, searchWayViewH) withSuperView:self.searchWayView withImageName:self.searchLineTitleArr[index][1] withTitleStr:self.searchLineTitleArr[index][0] withIndex:index];
        CGFloat buttonW = self.navBackView.width / 3;
        [[[UIView viewWithFrame:CGRectMake(buttonW * index, 0, buttonW, self.navBackView.height)] resetBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]] setupOnView:self.navBackView].tag = index + 30;
        UIButton * searchButton = [ViewFactory buttonWithFrame:CGRectMake(buttonW * index, 0, buttonW, self.navBackView.height) title:@"" normalColor:[UIColor clearColor] highlightColor:[UIColor clearColor] disabledColor:[UIColor clearColor] inView:self.navBackView action:nil];
        searchButton.tag = index + 20;
        [searchButton addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        if (index == 2) {
            [self searchButtonClick:searchButton];
        }
    }
    
    self.navigationItem.titleView = self.navBackView;
}
//- (void)addSearLineTypeWayView
//{
//    CGFloat searchWayViewW = __MAIN_WIDTH;
//    CGFloat searchWayViewH = 44;
//    self.searchWayView = [Factory viewWithFrame:CGRectMake(0, 0, searchWayViewW, searchWayViewH) bgColor:COLOR_HEXSTRING(@"#FFFFFF") onView:self.view];
//    self.searchLineTitleArr = [NSMutableArray arrayWithArray: @[@[@"步行",@"runImage",@"runSelectImage"],@[@"驾车",@"driverImage",@"driverSelectImage"],@[@"公交",@"busImage",@"busSelectImage"]]];
//    for (int index = 0; index < self.searchLineTitleArr.count; index ++) {
//        CGFloat lineH = 38;
//        CGFloat lineY = (searchWayViewH - lineH) / 2;
//        CGFloat lineW = 1;
//        CGFloat lineX = __MAIN_WIDTH / 3 * (index + 1);
//        if (index < self.searchLineTitleArr.count - 1) [Factory viewWithFrame:CGRectMake(lineX, lineY, lineW, lineH) bgColor:COLOR_HEXSTRING(@"#E8E8E8") onView:self.searchWayView];
//
//        CGFloat smallSearchViewW = __MAIN_WIDTH / 3;
//        CGFloat smallSearchViewX = smallSearchViewW * index;
//        UIView * smallSearchView = [self searchThreeView:CGRectMake(smallSearchViewX, 0, smallSearchViewW, searchWayViewH) withSuperView:self.searchWayView withImageName:self.searchLineTitleArr[index][1] withTitleStr:self.searchLineTitleArr[index][0] withIndex:index];
//        UIButton * searchButton = [ViewFactory buttonWithFrame:smallSearchView.frame title:@"" normalColor:[UIColor clearColor] highlightColor:[[Theme mainColor] colorWithAlphaComponent:0.3] disabledColor:[[Theme mainColor] colorWithAlphaComponent:0.5] inView:self.searchWayView action:nil];
//        searchButton.tag = index + 20;
//        [searchButton addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//
//    }
//}
- (void)searchButtonClick:(UIButton *)sender
{
    [self reloadSearchView];
    NSInteger tagNum = sender.tag - 20;
    //    UIView * smallSearchView = (UIView *)[self.searchWayView viewWithTag:21 + tagNum];
    //    UIImageView * selectImageV = (UIImageView *)[smallSearchView viewWithTag:33 + tagNum];
    //    [selectImageV setImage:ImageNamed(self.searchLineTitleArr[tagNum][2])];
    //    UILabel * selectLabel = (UILabel *)[smallSearchView viewWithTag:34 + tagNum];
    //    [selectLabel setTextColor:[Theme mainColor]];
    
    UIView * carView = (UIView *)[self.navBackView viewWithTag:30];
    UIView * butsView = (UIView *)[self.navBackView viewWithTag:31];
    UIView * runView = (UIView *)[self.navBackView viewWithTag:32];
    
    if (tagNum == 0)
    {
        [carView setBackgroundColor:[UIColor clearColor]];
        [butsView setBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]];
        [runView setBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]];
        
        self.searchLineType = AMapRoutePlanningTypeDrive;
    }
    if (tagNum == 1 )
    {
        [carView setBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]];
        [butsView setBackgroundColor:[UIColor clearColor]];
        [runView setBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]];
        
        self.searchLineType = AMapRoutePlanningTypeBus;
    }
    if (tagNum == 2 )
    {
        [carView setBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]];
        [butsView setBackgroundColor:[[Theme mainNavColor] colorWithAlphaComponent:0.6]];
        [runView setBackgroundColor:[UIColor clearColor]];
        
        self.searchLineType = AMapRoutePlanningTypeWalk;
    }

    [self.mapLocate wayToLoc:self.searchLineType];
    
}
- (void)reloadSearchView
{
    for (int index = 0 ; index < self.searchLineTitleArr.count; index ++) {
        UIView * smallSearchView = (UIView *)[self.searchWayView viewWithTag:21 + index];
        UIImageView * selectImageV = (UIImageView *)[smallSearchView viewWithTag:33 + index];
        [selectImageV setImage:ImageNamed(self.searchLineTitleArr[index][1])];
        UILabel * selectLabel = (UILabel *)[smallSearchView viewWithTag:34 + index];
        [selectLabel setTextColor:COLOR_HEXSTRING(@"#8a8a8a")];
    }
}
- (UIView *)searchThreeView:(CGRect)searchViewFrame withSuperView:(UIView *)superView withImageName:(NSString *)imageName withTitleStr:(NSString *)titleStr withIndex:(NSInteger)indexNum
{
    UIView * smallSearchView = [[[UIView viewWithFrame:searchViewFrame] resetBackgroundColor:[UIColor clearColor]] setupOnView:superView];
    smallSearchView.tag = 21 + indexNum;
    UIImage * iconImage = ImageNamed(imageName);
    UIImageView * iconImageV = [Factory imageViewWithCenter:CGPointMake(15 * __MAIN_RATIO_375 + iconImage.size.width / 2, searchViewFrame.size.height / 2) image:iconImage onView:smallSearchView];
    iconImageV.tag = indexNum + 33;
    UILabel * textLabel = [Factory labelWithFrame:CGRectMake(45 * __MAIN_RATIO_375, 0, 50, searchViewFrame.size.height) font:FONT_DEFAULT_Light(14) text:titleStr textColor:COLOR_HEXSTRING(@"#8a8a8a") onView:smallSearchView textAlignment:NSTextAlignmentLeft];
    textLabel.tag = indexNum + 34;
    return smallSearchView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.refreshMgr.refreshCarLocation startRefreshWithTimeInterval:0];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.refreshMgr.refreshCarLocation stopRefresh];
}

-(void)refreshCarLocation{
    self.mapLocate.coordinate = self.refreshMgr.refreshCarLocation.location;
    self.mapLocate.currentCity = self.refreshMgr.refreshCarLocation.cityNameStr;
}
@end
